/**
 * 作者: hxl
 * 日期: 2024-10-16 15:05:41
 * 备注: 秘书
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSecretary;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSecretaryModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.secretary_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // ID
    protected long userId; // 玩家ID
    protected int secretaryId; // 秘书ID
    protected int favorite; // 缘分值
    protected int charm; // 魅力值
    protected int suitId; // 服饰
    protected int identitySkillLevel; // 基因技能的升级次数 (每次升级不一定提升基因技能的等级)
    protected int energy; // 精力
    protected long recoverEnergyTime; // 上次恢复精力的时间
    protected int fetters; // 羁绊值
    protected String skillPatronLevel = ""; // 羁绊技能的升级情况
    protected String businessValue = ""; // 经营技能的加成汇总 (冗余方便计算用，CMD_GAME_SECRETARY_POTENTIAL_SKILL_LIST会重算)

    public void reset() {
    }

    protected String getKey() {
        return "UserSecretary." + userId + "." + secretaryId;
    }

    protected String getListKey() {
        return "UserSecretary." + userId;
    }

    protected String getListItemKey() {
        return secretaryId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_secretary"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("userId", userId); // 玩家ID
        obj.put("secretaryId", secretaryId); // 秘书ID
        obj.put("favorite", favorite); // 缘分值
        obj.put("charm", charm); // 魅力值
        obj.put("suitId", suitId); // 服饰
        obj.put("identitySkillLevel", identitySkillLevel); // 基因技能的升级次数 (每次升级不一定提升基因技能的等级)
        obj.put("energy", energy); // 精力
        obj.put("recoverEnergyTime", recoverEnergyTime); // 上次恢复精力的时间
        obj.put("fetters", fetters); // 羁绊值
        obj.put("skillPatronLevel", skillPatronLevel); // 羁绊技能的升级情况
        obj.put("businessValue", businessValue); // 经营技能的加成汇总 (冗余方便计算用，CMD_GAME_SECRETARY_POTENTIAL_SKILL_LIST会重算)
        return obj;
    }

    public PBUserSecretary.Builder takeInitBuilder() {
        PBUserSecretary.Builder sb = PBUserSecretary.newBuilder();
        if (secretaryId != 0) {
            sb.setSecretaryId(secretaryId);
        }
        if (favorite != 0) {
            sb.setFavorite(favorite);
        }
        if (charm != 0) {
            sb.setCharm(charm);
        }
        if (suitId != 0) {
            sb.setSuitId(suitId);
        }
        if (identitySkillLevel != 0) {
            sb.setIdentitySkillLevel(identitySkillLevel);
        }
        if (energy != 0) {
            sb.setEnergy(energy);
        }
        if (recoverEnergyTime != 0) {
            sb.setRecoverEnergyTime(recoverEnergyTime);
        }
        if (fetters != 0) {
            sb.setFetters(fetters);
        }
        if (this.skillPatronLevel != null && this.skillPatronLevel.length() > 0) {
            JsonArray list = new JsonArray(this.skillPatronLevel);
            for(int i=0; i<list.size(); i++) {
                sb.addSkillPatronLevel(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserSecretary.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSecretary.Builder sb = PBUserSecretary.newBuilder();
        if (isFieldUpdate("secretaryId")) {
            sb.setSecretaryId(secretaryId);
        }
        if (isFieldUpdate("favorite")) {
            sb.setFavorite(favorite);
        }
        if (isFieldUpdate("charm")) {
            sb.setCharm(charm);
        }
        if (isFieldUpdate("suitId")) {
            sb.setSuitId(suitId);
        }
        if (isFieldUpdate("identitySkillLevel")) {
            sb.setIdentitySkillLevel(identitySkillLevel);
        }
        if (isFieldUpdate("energy")) {
            sb.setEnergy(energy);
        }
        if (isFieldUpdate("recoverEnergyTime")) {
            sb.setRecoverEnergyTime(recoverEnergyTime);
        }
        if (isFieldUpdate("fetters")) {
            sb.setFetters(fetters);
        }
        if (isFieldUpdate("skillPatronLevel")) {
            if (this.skillPatronLevel != null && this.skillPatronLevel.length() > 0) {
                JsonArray list = new JsonArray(this.skillPatronLevel);
                for(int i=0; i<list.size(); i++) {
                    sb.addSkillPatronLevel(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearSkillPatronLevel();
                }
            } else {
                sb.clearSkillPatronLevel();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getSecretaryId() {
        return secretaryId;
    }

    public void putSecretaryId(int secretaryId) {
        if (this.secretaryId != secretaryId) {
            this.secretaryId = secretaryId;
            updateRedisHashField(getKey(), "secretaryId", this.secretaryId);
        }
    }

    public void addSecretaryId(int value) {
        this.secretaryId += value;
        updateRedisHashField(getKey(), "secretaryId", this.secretaryId);
    }

    public int getFavorite() {
        return favorite;
    }

    public void putFavorite(int favorite) {
        if (this.favorite != favorite) {
            this.favorite = favorite;
            updateRedisHashField(getKey(), "favorite", this.favorite);
        }
    }

    public void addFavorite(int value) {
        this.favorite += value;
        updateRedisHashField(getKey(), "favorite", this.favorite);
    }

    public int getCharm() {
        return charm;
    }

    public void putCharm(int charm) {
        if (this.charm != charm) {
            this.charm = charm;
            updateRedisHashField(getKey(), "charm", this.charm);
        }
    }

    public void addCharm(int value) {
        this.charm += value;
        updateRedisHashField(getKey(), "charm", this.charm);
    }

    public int getSuitId() {
        return suitId;
    }

    public void putSuitId(int suitId) {
        if (this.suitId != suitId) {
            this.suitId = suitId;
            updateRedisHashField(getKey(), "suitId", this.suitId);
        }
    }

    public void addSuitId(int value) {
        this.suitId += value;
        updateRedisHashField(getKey(), "suitId", this.suitId);
    }

    public int getIdentitySkillLevel() {
        return identitySkillLevel;
    }

    public void putIdentitySkillLevel(int identitySkillLevel) {
        if (this.identitySkillLevel != identitySkillLevel) {
            this.identitySkillLevel = identitySkillLevel;
            updateRedisHashField(getKey(), "identitySkillLevel", this.identitySkillLevel);
        }
    }

    public void addIdentitySkillLevel(int value) {
        this.identitySkillLevel += value;
        updateRedisHashField(getKey(), "identitySkillLevel", this.identitySkillLevel);
    }

    public int getEnergy() {
        return energy;
    }

    public void putEnergy(int energy) {
        if (this.energy != energy) {
            this.energy = energy;
            updateRedisHashField(getKey(), "energy", this.energy);
        }
    }

    public void addEnergy(int value) {
        this.energy += value;
        updateRedisHashField(getKey(), "energy", this.energy);
    }

    public long getRecoverEnergyTime() {
        return recoverEnergyTime;
    }

    public void putRecoverEnergyTime(long recoverEnergyTime) {
        if (this.recoverEnergyTime != recoverEnergyTime) {
            this.recoverEnergyTime = recoverEnergyTime;
            updateRedisHashField(getKey(), "recoverEnergyTime", this.recoverEnergyTime);
        }
    }

    public void addRecoverEnergyTime(long value) {
        this.recoverEnergyTime += value;
        updateRedisHashField(getKey(), "recoverEnergyTime", this.recoverEnergyTime);
    }

    public int getFetters() {
        return fetters;
    }

    public void putFetters(int fetters) {
        if (this.fetters != fetters) {
            this.fetters = fetters;
            updateRedisHashField(getKey(), "fetters", this.fetters);
        }
    }

    public void addFetters(int value) {
        this.fetters += value;
        updateRedisHashField(getKey(), "fetters", this.fetters);
    }

    public JsonArray getSkillPatronLevelArray() {
        if (this.skillPatronLevel != null && this.skillPatronLevel.length() > 0) {
            return new JsonArray(this.skillPatronLevel);
        } else {
            return new JsonArray();
        }
    }

    public String getSkillPatronLevel() {
        return skillPatronLevel;
    }

    public void putSkillPatronLevel(String skillPatronLevel) {
        if (!this.skillPatronLevel.equals(skillPatronLevel)) {
            this.skillPatronLevel = skillPatronLevel;
            updateRedisHashField(getKey(), "skillPatronLevel", this.skillPatronLevel);
        }
    }

    public JsonArray getBusinessValueArray() {
        if (this.businessValue != null && this.businessValue.length() > 0) {
            return new JsonArray(this.businessValue);
        } else {
            return new JsonArray();
        }
    }

    public String getBusinessValue() {
        return businessValue;
    }

    public void putBusinessValue(String businessValue) {
        if (!this.businessValue.equals(businessValue)) {
            this.businessValue = businessValue;
            updateRedisHashField(getKey(), "businessValue", this.businessValue);
        }
    }

}